<?php
include_once('./config/config.php');
// 接收参数id
$id = $_GET['id'];
// 通过id获取信息
$sql = "SELECT p.*, d.name as dname, j.name as jname from person as p 
        LEFT JOIN department as d ON p.depid = d.id 
        LEFT JOIN job as j ON p.jobid = j.id where p.id = '$id'";

$admin = findOne($sql);

// 获取所有部门数据
$depRes = findAll("select * from department");
// 获取所有职位数据
if (!$depRes) {
  ShowMsg("depRes数据正在紧急修复中...");
}

if ($_POST) {
  // 接收全部参数
  $params = $_POST;
  // var_dump($params);
  $action = $params['action'];
  if ($action == 'depid') {
    $depid = $_POST['depid'];
    $depRes = findAll("select * from job where depid = '$depid'");
    if ($depRes) {
      success("查询成功", $depRes);
    } else {
      error("暂无数据");
    }
  }
  $data = [
    'name' => $params['name'],
    'sex' => $params['sex'],
    'mobile' => $params['mobile'],
    'email' => $params['email'],
    'address' => $params['address'],
    'depid' => $params['depid'],
    'jobid' => $params['jobid']
  ];
  if ($_FILES['avatar'] && $_FILES['avatar']['size'] > 0) {
    // 调用上传函数
    $avatar = upload('avatar');
    if ($avatar['code'] == 0) {
      ShowMsg($avatar['msg']);
    } else {
      $data['avatar'] = $avatar['data'];
    }
  }
  if ($data['jobid'] != 0) {
    $res = update('person', $data, "`id` = $id");
    if ($res) {
      if (isset($data['avatar']) && $_FILES['avatar']['size'] > 0) {
        @is_file($data['avatar']) && @unlink($admin['avatar']);
      }
      ShowMsg('员工更新成功', 'personList.php');
      exit;
    } else {
      @is_file($data['avatar']) && @unlink($data['avatar']);
      ShowMsg('员工更新失败');
      exit;
    }
  } else {
    ShowMsg("该部门暂无职位，无法选择，请重试.", "personList.php");
  }
 
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
  <!-- 引入 -->
  <?php include_once('./common/meta.php') ?>
</head>

<style>
  .avatar_lab {
    width: 200px;
  }

  .avatar_img {
    width: 200px;
  }
</style>

<body>
  <!-- 引入头部导航 -->
  <?php include_once('./common/navbar.php') ?>
  <!-- 引入侧边菜单栏 -->
  <?php include_once('./common/sidebar.php') ?>

  <div class="content">
    <div class="header">
      <h1 class="page-title">编辑员工</h1>
    </div>
    <ul class="breadcrumb">
      <li><a href="index.php">Home</a> <span class="divider">/</span></li>
      <li class="active">editPerson</li>
    </ul>

    <div class="container-fluid">
      <div class="row-fluid">
        <div class="btn-toolbar">
          <button class="btn btn-primary" onClick="location='personList.php'"><i class="icon-list"></i> 返回列表</button>
          <div class="btn-group">
          </div>
        </div>

        <div class="well">
          <div id="myTabContent" class="tab-content">
            <div class="tab-pane active in" id="home">
              <form method="POST" enctype="multipart/form-data">

                <!-- 员工名称 -->
                <label>员工名称</label>
                <input type="text" class="input-xxlarge" name="name" value="<?php echo $admin['name'] ?>" required>
                <span class="msg"></span>
                <br>
                <!-- 性别 -->
                <input type="radio" name="sex" id="sex" value="0" <?php echo $admin['sex'] == 0 ? 'checked' : '' ?> >保密
                <input type="radio" name="sex" id="sex" value="1" <?php echo $admin['sex'] == 1 ? 'checked' : '' ?> >男
                <input type="radio" name="sex" id="sex" value="2" <?php echo $admin['sex'] == 2 ? 'checked' : '' ?> >女
                <br>
                <br>
                <!-- 员工电话 -->
                <label>员工电话</label>
                <input type="text" class="input-xxlarge" name="mobile" value="<?php echo $admin['mobile'] ?>" required>
                <!-- 员工邮箱 -->
                <label>员工邮箱</label>
                <input type="text" class="input-xxlarge" name="email" value="<?php echo $admin['email'] ?>" required>
                <!-- 员工地址 -->
                <label>员工地址</label>
                <input type="text" class="input-xxlarge" name="address" value="<?php echo $admin['address'] ?>">
                <!-- 员工部门 -->
                <label>请选择部门</label>
                <select name="depid" id="depid">
                  <option value="0">请选择</option>
                  <?php foreach ($depRes as $item) { ?>
                    <option value="<?php echo $item['id'] ?>" <?php echo $admin['depid'] == $item['id'] ? "selected" : '' ?> > <?php echo $item['name'] ?> </option>
                  <?php } ?>
                </select>
                <!-- 职位 -->
                <label>请选择职位</label>
                <select name="jobid" id="job">
                  <option value="<?php echo $admin['id'] ?>"> <?php echo $admin['jname'] ?> </option>
                </select>
                <!-- 员工邮箱 -->
                <!-- 头像 -->
                <label>头像</label>
                <input type="file" name="avatar" id="avatar" hidden>
                <label for="avatar" class="avatar_lab">
                  <img src="<?php echo $admin['avatar'] ?>" alt="" id="avatar_img">
                </label>

                <button class="btn btn-primary" type="submit" value="提交">提交</button>
              </form>
            </div>
          </div>
        </div>

        <!-- 引入底部 -->
        <?php include_once('./common/footer.php') ?>

      </div>
    </div>
  </div>

</body>

</html>
<!-- 引用公共js文件 -->
<?php include_once('./common/script.php') ?>
<script>
  // 图片预览函数
  function GetObjectURL(file) {
    // createObjectURL  给一个文件对象 可以提取出一个url本地地址出来
    var url = null;

    //判断是否为IE浏览器还是google还是其他浏览器
    if (window.createObjectURL != undefined) {
      url = window.createObjectURL(file)
    } else if (window.URL != undefined) {
      url = window.URL.createObjectURL(file)
    } else if (window.webkitURL != undefined) {
      url = window.webkitURL.createObjectURL(file)
    }

    return url
  }

  $('#avatar').change(function() {
    // 获取选中的文件
    let file = $(this)[0].files[0] ? $(this)[0].files[0] : {}
    // 获取该文件地址
    let src = GetObjectURL(file)
    // console.log(src)
    $('#avatar_img').attr('src', src)
  })

  $("#depid").change(function () {
    // console.log($(this).val());
    let depid = $(this).val()
    $.ajax({
      type: "post",
      url: "editPerson.php",
      data: {
        depid,
        action: 'depid'
      },
      dataType: 'json',
      success: function(res) {
        console.log(res)
        let option = ''
        if (res.code == 1) {
          res.data.forEach(item => {
            option += `<option value=${item.id}> ${item.name} </option>`
          })
          $("#job").html(option)
        } else {
          option += `<option value='0'> 请选择 </option>`
          $("#job").html(option)
        }
      },
      error: function(err) {
        console.log(err)
      }
    })
  })
</script>